package com.hd123.rumba.request;

import java.util.Hashtable;

import javax.servlet.http.HttpSession;

import com.hd123.rumba.domain.IDomain;
import com.hd123.rumba.organization.IOrganization;
import com.hd123.rumba.user.IUser;

public class BuffaloSession {

  private static final ThreadLocal<HttpSession> sessionLocal = new ThreadLocal();

  /**
   * @param session
   *          caller ensure it is not null
   */
  public static void setSession(HttpSession session) {
    Hashtable sessionValues = (Hashtable) session.getAttribute(Dictionary.SESSIONMARKER);
    if (sessionValues == null) {
      sessionValues = new Hashtable();
      session.setAttribute(Dictionary.SESSIONMARKER, sessionValues);
    }
    sessionLocal.set(session);
  }

  public static void removeSession() {
    sessionLocal.remove();
  }

  public static IDomain getCurrentDomain() {
    return (IDomain) getSessionValues().get(Dictionary.CURRENTDOMAIN);
  }

  public static void setCurrentDomain(IDomain domain) {
    getSessionValues().put(Dictionary.CURRENTDOMAIN, domain);
  }

  public static IUser getCurrentUser() {
    return (IUser) getSessionValues().get(Dictionary.CURRENTUSER);
  }

  public static void setCurrentUser(IUser user) {
    getSessionValues().put(Dictionary.CURRENTUSER, user);
  }

  public static IDomain getEnterpriseDomain() {
    return (IDomain) getSessionValues().get(Dictionary.ENTERPRISEDOMAIN);
  }

  public static void setEnterpriseDomain(IDomain domain) {
    getSessionValues().put(Dictionary.ENTERPRISEDOMAIN, domain);
  }

  public static IOrganization getCurrentOrganization() {
    return (IOrganization) getSessionValues().get(Dictionary.CURRENTORGANIZATION);
  }

  public static void setCurrentOrganization(IOrganization org) {
    getSessionValues().put(Dictionary.CURRENTORGANIZATION, org);
  }

  // public static IDomain getLoginDomain() {
  // return (IDomain) getSessionValues().get(Dictionary.LOGINDOMAIN);
  // }
  //
  // public static void setLoginDomain(IDomain domain) {
  // getSessionValues().put(Dictionary.LOGINDOMAIN, domain);
  // }
  //
  // public static IUser getLoginUser() {
  // return (IUser) getSessionValues().get(Dictionary.LOGINUSER);
  // }
  //
  // public static void setLoginUser(IUser user) {
  // getSessionValues().put(Dictionary.LOGINUSER, user);
  // }

  public static Hashtable getSessionValues() {
    return (Hashtable) sessionLocal.get().getAttribute(Dictionary.SESSIONMARKER);
  }

  public static HttpSession getSession() {
    return (HttpSession) sessionLocal.get();
  }
}
